﻿using ModbusCase.Lib;
using System.IO.Ports;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ModbusCase
{
    /// <summary>
    /// 未优化的Modbus RTU通信示例测试
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Btn01_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTU modbusRTU = new ModbusRTU(serialPort, 0x01);
            modbusRTU.ReadCoils(0, 3);
            //01 01 00 00 00 03 7C 0B
            //01 01 01 05 91 8B
        }

        private void Btn02_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTU modbusRTU = new ModbusRTU(serialPort, 0x01);
            bool[] rst = modbusRTU.ReadDiscreteInput(0, 5);
        }

        private void Btn03_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTU modbusRTU = new ModbusRTU(serialPort, 0x01);
            modbusRTU.ReadHoldingReg16(0, 3);
        }

        private void Btn03_32_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTU modbusRTU = new ModbusRTU(serialPort, 0x01);
            //modbusRTU.ReadHoldingReg32(0, 3,Endian.BigEndian);
            //modbusRTU.ReadHoldingReg32(0, 3, Endian.LittleEndian);

            //modbusRTU.ReadHoldingReg32Float(0, 3, Endian.LittleEndian);
            modbusRTU.ReadHoldingReg32Float(0, 3, Endian.BigEndian);
        }

        private void btn05_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTU modbusRTU = new ModbusRTU(serialPort, 0x01);
            modbusRTU.WriteSingleCoil(3, true);
        }

        private void btn0F_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTU modbusRTU = new ModbusRTU(serialPort, 0x01);
            //modbusRTU.WriteMultipleCoil(0, [true, false, true, true]); 
            //modbusRTU.WriteMultipleCoil(0, [true, false, true, true, true]); // 1 1101<= 16+8+4+1=29 0x1D 
            modbusRTU.WriteMultipleCoil(1, [true, true, true, false, false, true, true, false, true]);
        }

        private void btn06_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTU modbusRTU = new ModbusRTU(serialPort, 0x01);
            modbusRTU.WriteSingleReg(9, 456);
        }

        private void btn10_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTU modbusRTU = new ModbusRTU(serialPort, 0x01);
            //modbusRTU.WriteMultipleReg32Int(2, [123, 456, 789],Endian.BigEndian);

            modbusRTU.WriteMultipleReg32Int(2, [123, 456, 789], Endian.LittleEndian);

            //测试异常(modbus slave只开了10寄存器,异常(非法的数据地址))
            // 01 10 00 07 00 04 08 00 00 00 7B 00 00 01 C8 A7 BD
            // 01 90 02 CD C1
            //modbusRTU.WriteMultipleReg32Int(7, [123, 456]);
        }

        private void btn10_float_Click(object sender, RoutedEventArgs e)
        {
            SerialPort serialPort = new SerialPort("COM19", 9600, Parity.None, 8, StopBits.One);
            serialPort.ReadTimeout = 500; // 设置读取超时时间,单位毫秒，避免死锁(读不到就不要读了)
            serialPort.Open();

            ModbusRTU modbusRTU = new ModbusRTU(serialPort, 0x01);
            modbusRTU.WriteMultipleReg32Float(2, [123.1F, 456.5F, 789.6F], Endian.BigEndian);

            //modbusRTU.WriteMultipleReg32Float(2, [123.1F, 456.5F, 789.6F], Endian.LittleEndian);
        }
    }
}